<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="650px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="100px">
      <el-form-item label="审核" prop="verifyStatus" required>
        <el-radio-group v-model="form.verifyStatus">
          <el-radio
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核备注" prop="verifyExplain" :required="form.verifyStatus === '2'">
        <el-input
          type="textarea"
          :rows="3"
          placeholder="请输入审核备注"
          v-model="form.verifyExplain"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <SetCooperation v-if="form.verifyStatus === '1'" ref="SetCooperation" />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm"
        >{{ form.verifyStatus === '1' ? '通过并设置合作' : '确认驳回' }} </el-button
      >
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateApplyCooperate } from "@/api/facilitator";
import SetCooperation from '@/views/channel/myChannel/setCooperation';

export default {
  components: { SetCooperation },
  dicts: ['audit_status'],
  data() {
    return {
      title: "合作申请审核",
      open: false,
      form: {
        id: undefined,
        verifyStatus: '1', // 1通过 2驳回
        verifyExplain: "",
        tcmChannelCooperate: {
          channelId: undefined,  // 事业部ID
        }
      },
    };
  },
  methods: {
    openModal(row) {
      this.open = true;
      this.form.id = row.id;
      this.form.tcmChannelCooperate.channelId = row.channelId;
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    async submitForm() {
      const that = this;
      if(that.form.verifyStatus === '1'){
        that.form.tcmChannelCooperate = { ...that.$refs.SetCooperation.tcmChannelCooperate, ...that.form.tcmChannelCooperate };
        that.form.tcmChannelCooperate.priceControl = that.form.tcmChannelCooperate.cooperateType === 1 ? 1 : that.form.tcmChannelCooperate.priceControl; // 统一定价返佣,强制设置为控价
      }
      that.$refs["form"].validate(async (valid) => {
        if(valid){
          await updateApplyCooperate(that.form);
          that.$message.success("审核成功");
          that.$emit("refresh");
          that.closeModal();
        }
      })
    }
  },
};
</script>

<style lang="scss" scoped></style>
